package com.ykdl.member.views;

import java.text.DateFormat.Field;
import android.R.attr;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Camera;
import android.opengl.Matrix;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Scroller;
import android.widget.AdapterView.OnItemClickListener;

	public class MyGallery extends Gallery {
		private String TAG = this.getClass().getSimpleName();
		private Camera mCamera;
		private int mWidth;
		private int mPaddingLeft;
		private int mPaddingRight;
		private static int firstChildWidth;
		private static int firstChildPaddingLeft;
		private int offsetX;
		private Scroller mScroller;
		private int mAnimationDuration = 400;

		@SuppressLint("NewApi")
		public MyGallery(Context context) {
			super(context);
			mCamera = new Camera();
			mScroller = new Scroller(context);
			this.setStaticTransformationsEnabled(true);
		}

		@SuppressLint("NewApi")
		public MyGallery(Context context, AttributeSet attrs) {
			super(context, attrs);
			mCamera = new Camera();
			mScroller = new Scroller(context);
			setAttributesValue(context, attrs);
			this.setStaticTransformationsEnabled(true);
		}

		@SuppressLint("NewApi")
		public MyGallery(Context context, AttributeSet attrs, int defStyle) {
			super(context, attrs, defStyle);
			mCamera = new Camera();
			mScroller = new Scroller(context);
			setAttributesValue(context, attrs);
			this.setStaticTransformationsEnabled(true);
		}

		private void setAttributesValue(Context context, AttributeSet attrs) {
			TypedArray typedArray = context.obtainStyledAttributes(attrs, new int[] {attr.paddingLeft, attr.paddingRight});
			mPaddingLeft = typedArray.getDimensionPixelSize(0, 0);
			mPaddingRight = typedArray.getDimensionPixelSize(1, 0);
			typedArray.recycle();
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			System.out.println("拦截单击自动滚动事件");
			int mDownTouchPosition = pointToPosition((int) e.getX(), (int) e.getY());
			int mFirstPosition = this.getSelectedItemPosition();
			View mDownTouchView = getChildAt(mDownTouchPosition - mFirstPosition);
			if (mDownTouchPosition >= 0) {
				if (listener != null) {
					listener.onItemClick(this, mDownTouchView, mDownTouchPosition, mDownTouchView.getId());
				}
			}
			return false;
		}

		private OnItemClickListener listener;

		@Override
		public void setOnItemClickListener(OnItemClickListener listener) {
			this.listener = listener;
			super.setOnItemClickListener(listener);
		}

		protected boolean getChildStaticTransformation(View child, Transformation t) {
			t.clear();
			t.setTransformationType(Transformation.TYPE_MATRIX);
			mCamera.save();
			final android.graphics.Matrix imageMatrix = t.getMatrix();
			if (flag) {
				firstChildWidth = getChildAt(0).getWidth();
				firstChildPaddingLeft = getChildAt(0).getPaddingLeft();
				flag = false;
			}
			offsetX = firstChildWidth / 2 + firstChildPaddingLeft + mPaddingLeft - mWidth / 2;
			mCamera.translate(offsetX, 0f, 0f);
			mCamera.getMatrix(imageMatrix);
			mCamera.restore();
			return true;
		}

		private int getCenterOfGallery() {
			return (getWidth() - mPaddingLeft - mPaddingRight) / 2 + mPaddingLeft;
		}

		private static int getCenterOfView(View view) {
			return view.getLeft() + view.getWidth() / 2;
		}

		private void scrollIntoSlots() {
			new Handler().post(new Runnable() {
				@Override
				public void run() {
					int selectedCenter = getCenterOfView(MyGallery.this.getSelectedView());
					int targetCenter = getCenterOfGallery();

					int distance = targetCenter - selectedCenter;
					System.out.println("selectedCenter =" + selectedCenter);
					System.out.println("targetCenter =" + targetCenter);
					System.out.println("distance =" + distance);
//	 				mScroller.startScroll(0, 0, -distance, 0, mAnimationDuration);
				}
			});
			System.out.println("自动滑动");
		}

		void onUp() {
			scrollIntoSlots();
			dispatchUnpress();
			postInvalidate();
		}

		private void dispatchUnpress() {

			for (int i = getChildCount() - 1; i >= 0; i--) {
				getChildAt(i).setPressed(false);
			}

			setPressed(false);
		}

		int downx = 0;
		int downy = 0;
		int mDownTouchPosition = -1;

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			event.offsetLocation(-offsetX, 0);
			if (mDownTouchPosition == -1) {
				mDownTouchPosition = pointToPosition((int) event.getX(), (int) event.getY());
			}
			// View mDownTouchView = getChildAt(mDownTouchPosition);
			if (mDownTouchPosition >= 0) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					Log.d(TAG, "mDownTouchView!=nul MotionEvent.ACTION_UP=" + event.getAction());
					onUp();
				}
				if (event.getAction() == MotionEvent.ACTION_CANCEL) {
					Log.d(TAG, "mDownTouchView!=nul MotionEvent.ACTION_CANCEL=" + event.getAction());
					onUp();
				}
				Log.d(TAG, "mDownTouchPosition = " + mDownTouchPosition);
				mDownTouchPosition = -1;
			}

			Log.d(TAG, "MotionEvent = " + event.getAction());
			return super.onTouchEvent(event);
		}

		private boolean flag;
		private int w = -1;

		protected void onSizeChanged(int w, int h, int oldw, int oldh) {
			System.out.println("mydemo: onSizeChanged");
			System.out.println("mydemo: w=" + w);
			System.out.println("mydemo: this. w=" + this.w);
			System.out.println("mydemo: oldw=" + oldw);
			if (this.w == -1) {
				this.w = w;
				mWidth = this.w * 2;
				flag = true;
				getLayoutParams().width = mWidth;
				System.out.println("mydemo: mWidth=" + mWidth);
			}
		}

		public class MyOnItemClickListener implements OnItemClickListener {
			public AdapterView<?> parent;
			public View view;
			public int position;
			public long id;

			public MyOnItemClickListener(AdapterView<?> parent, View view, int position, long id) {
				super();
				// TODO Auto-generated constructor stub
			}

			public void itemClicked() {
				// TODO Auto-generated method stub
				onItemClick(parent, view, position, id);
			}

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub

			}
		}
	}
